from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 添加物料清单函数
def add_material(material_data):
    # 跳转至新物料清单界面
    browser.find_element(By.ID, "mainmenua_mrp").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_bom_bom_card .vsmenu").click()

    # 标签
    browser.find_element(By.ID, "label").send_keys(material_data["label"])

    # 类型（child 2, 3）
    browser.find_element(By.CSS_SELECTOR, f"select#bomtype>option:nth-child({material_data['bomtype']})").click()

    # 产品（child 2, 3, 4, 5）
    browser.find_element(By.CSS_SELECTOR, f"select#fk_product>option:nth-child({material_data['fk_product']})").click()

    # 数量
    browser.find_element(By.ID, "qty").send_keys(str(material_data["qty"]))

    # 描述
    browser.find_element(By.ID, "description").send_keys(material_data["description"])

    # 估计时间
    browser.find_element(By.NAME, "durationhour").send_keys(str(material_data["durationhour"]))
    browser.find_element(By.NAME, "durationmin").send_keys(str(material_data["durationmin"]))

    # 生产仓库
    browser.find_element(By.CSS_SELECTOR, f"select#fk_warehouse>option:nth-child({material_data['fk_warehouse']})").click()

    # 创建按钮
    browser.find_element(By.NAME, "add").click()

if __name__ == '__main__':
    try:
        login()
        # 测试数据（3正向+3反向，共6个用例）
        # 测试结果：正向全部加进去，反向第一个添加失败，提示标签不可为空，第二个添加成功，数量自己修改为1，第三个正常添加
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "label": "手机主板生产BOM",
                "bomtype": 3,  # 假设3为生产类型
                "fk_product": 2,  # 关联第二个产品
                "qty": 100,
                "description": "手机主板生产物料清单，含100个组件",
                "durationhour": 5,
                "durationmin": 30,
                "fk_warehouse": 2  # 第二个仓库
            },
            {
                "label": "新产品研发BOM",
                "bomtype": 2,  # 假设2为研发类型
                "fk_product": 5,  # 关联第五个产品
                "qty": 20,
                "description": "新产品研发阶段物料清单，含20个试验组件",
                "durationhour": 8,
                "durationmin": 15,
                "fk_warehouse": 3  # 第三个仓库
            },
            {
                "label": "电脑组装BOM",
                "bomtype": 3,  # 假设4为组装类型
                "fk_product": 4,  # 关联第三个产品
                "qty": 50,
                "description": "电脑组装物料清单，含50个零部件",
                "durationhour": 3,
                "durationmin": 45,
                "fk_warehouse": 2 # 第一个仓库
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "label": "",  # 空标签
                "bomtype": 3,
                "fk_product": 2,
                "qty": 100,
                "description": "测试空标签",
                "durationhour": 5,
                "durationmin": 30,
                "fk_warehouse": 2,
                "expected_result": "添加失败，提示\"标签不能为空\""
            },
            {
                "label": "非法数量BOM",
                "bomtype": 3,
                "fk_product": 2,
                "qty": -10,  # 负数量
                "description": "测试负数量输入",
                "durationhour": 5,
                "durationmin": 30,
                "fk_warehouse": 2,
                "expected_result": "添加失败，提示\"数量不能为负数\""
            },
            {
                "label": "超长描述BOM",
                "bomtype": 3,
                "fk_product": 2,
                "qty": 100,
                "description": "a" * 501,  # 501个字符的超长描述
                "durationhour": 5,
                "durationmin": 30,
                "fk_warehouse": 2,
                "expected_result": "添加失败，提示\"描述长度不能超过500字符\""
            }
        ]
        for data in test_data:
            add_material(data)
            sleep(1)  # 等待页面加载
        #全选框
        browser.find_element(By.CSS_SELECTOR, ".menu_contenu_bom_bom_list .vsmenu").click()
        browser.find_element(By.ID,"checkforselects").click()
        sleep(1)
        #设置为生效
        browser.find_element(By.CSS_SELECTOR,"select#massaction>option:nth-child(2)").click()
        #确认
        browser.find_element(By.NAME,"confirmmassaction").click()


    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()